<?php

namespace McWin\Model;
use Common\Model\CommonModel;
import('Common.myfunc');

/*
 * 	押注记录页面.
 */

class CccBetModel extends CommonModel {

	protected $trueTableName = 'c_bet';
	
	/*
	*	某一个玩法的大家总押注.
	*/
	public function cal_money_of_wanfa($type, $qishu, $wanfa, $wanfa_xx){
		$sql = "SELECT SUM(money) AS betmoney FROM c_bet WHERE type='" . $type . "' AND qishu=$qishu AND mingxi_1='" . $wanfa . "' AND mingxi_2='" . $wanfa_xx . "'"; 
		$query = $this -> query($sql);
		$rs = $query->fetch_array();
		return $rs['betmoney'];	
	}

	/**
	 * 一个玩家一种玩法的总押注.
	 * @param  [type] $wanfa [description]
	 * @param  [type] $h     [description]
	 * @return [type]        [description]
	 */
	public function cal_money_of_wanfa_of_user($uid, $type, $qishu, $wanfa, $wanfa_xx){
		$sql = "SELECT SUM(money) AS ubetmoney FROM c_bet WHERE uid=$uid AND type='".$type."' AND qishu=$qishu AND mingxi_1='" . $wanfa . "' AND mingxi_2='" . $wanfa_xx . "'";
        $query = $this -> query($sql);
        $rs = $query -> fetch_array();
        return $rs['ubetmoney'];
	}

	/**
	 * [add_bet description]
	 * @param [type] $did      [description]
	 * @param [type] $uid      [description]
	 * @param [type] $username [description]
	 * @param [type] $addtime  [description]
	 * @param [type] $type     [description]
	 * @param [type] $qishu    [description]
	 * @param [type] $mingxi_1 [description]
	 * @param [type] $mingxi_2 [description]
	 * @param [type] $odds     [description]
	 * @param [type] $money    [description]
	 * @param [type] $win      [description]
	 * @param [type] $js       [description]
	 * @param [type] $bet_date [description]
	 * @param [type] $jieguo   [description]
	 * @param [type] $fs       [description]
	 */
	public function add_bet($did,$uid,$username,$addtime,$type,$qishu,$mingxi_1,$mingxi_2,$odds,$money,$win,$js,$bet_date,$jieguo,$fs){
		$data['did'] = $did;
		$data['uid'] = $uid;

		$data['username'] = $username;

		$data['addtime'] = $addtime;
		$data['type'] = $type;
		$data['qishu'] = $qishu;

		$data['mingxi_1'] = $mingxi_1;
		$data['mingxi_2'] = $mingxi_2;

		$data['odds'] = $odds;
		$data['money'] = $money;
		$data['win'] = $win;

		$data['js'] = $js;
		$data['bet_date'] = $bet_date;
		$data['jieguo'] = $jieguo;

		$data['fs'] = $fs;
	}


	/**
	 * 结果计算后, 更新
	 * @param  [type] $id     [description]
	 * @param  [type] $is_win [description]
	 * @param  [type] $win    [description]
	 * @param  [type] $jieguo [description]
	 * @return [type]         [description]
	 */
	public function update_bet($id, $is_win, $win, $jieguo){
		$where['id'] = $id;

		$data['js'] = 1;
		$data['jieguo'] = $jieguo;

		if(!$is_win)
			$data['win'] = 0 - $win;

		$this -> where($where) -> save($data);
	}


		/**
		 * 读取要结算的数据.
		 * @param  [type] $type [description]
		 * @return [type]       [description]
		 */
		public function read_bets_for_js($type){
			$where['type'] = $type;
			$where['js'] = 0;

			$data = $this -> where($where) -> order('qishu desc');
			
	        return $data;
		}
	

	/**
	 * 查找某个玩家的未结注单
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function read_bets_unfinished_of_user($uid){
			$where['uid'] = $uid;
			$where['js'] = 0;

			$data = $this -> where($where) ->select();
			
	        return $data;
		}

	/**
	 * 读指定天数内的历史注单.
	 * @param  [type] $uid      [description]
	 * @param  [type] $weekDays [description]
	 * @return [type]           [description]
	 */
	public function read_bets_his_of_user($uid, $weekDays){
			$where['uid'] = $uid;
			$where['js'] = 1;

			$stime	=	date("Y-m-d"); //今天日期
			$etime	=	date("Y-m-d", strtotime("$stime - $weekDays day")); //前 $weekDays 天日期

			$where['bet_date'] = array(array('egt',$stime),array('elt',$etime));
			$data = $this -> where($where) ->select();
			
			$arr = array(); //单式			
			
			// 循环计算, 计算单日的总押分, 总盈利. 总盈亏
			if($data){
				for($i = 0; $i < count($data); $i++){
					// 单日总押分
					$arr['touZhu'][date('Y-m-d',strtotime($data[$i]['addtime']))] += $data[$i]['money'];

					// 反水
					$fs		=	$data[$i]['fs'];

					// 盈利
					if($data[$i]['js']=="1" && $data[$i]['win']>0){ //赢和赢一半都算赢
						$arr['win'][date('Y-m-d',strtotime($data[$i]['addtime']))]+=$data[$i]['win'] + $fs; //净赢金额不包括本金

					// 输
					}elseif($data[$i]['js']=="1" && $data[$i]['win']<0){ //输和输一半都算输
						$arr['lost'][date('Y-m-d',strtotime($data[$i]['addtime']))] += abs($data[$i]['win']) - $fs; //净输金额不包括已赢金额

					}else{ //平手和无效都算取消
						$arr['cancel'][date('Y-m-d',strtotime($data[$i]['addtime']))] += $data[$i]['money'];
					}
				}
			}

	        return $arr;
		}		


		/**
		 * 读取用户某一天的详细注单.
		 * @param  [type] $uid      [description]
		 * @param  [type] $bet_date [description]
		 * @return [type]           [description]
		 */
		public function read_bets_xx_of_user($uid, $bet_date){
			$where['uid'] = $uid;
			$where['js'] = 1;
			$where['bet_date'] = $bet_date;

			$data = $this -> where($where) -> order('addtime desc') ->select();
			
	        return $data;
		}		

}